#include "lib.h"
#include "math.h"

int lib_gsl_bsearch ( gsl_vector *array, double key, double key_deriviation, int low, int high )
{
    int middle;
    if ( low > high ) /* termination case */
        return -1;
    middle = high + (low - high) / 2;
    if ( fabs( gsl_vector_get( array, middle ) - key ) < key_deriviation )
        return middle;
    else if ( gsl_vector_get( array, middle ) - key_deriviation > key )
        return lib_gsl_bsearch( array, key, key_deriviation, low, middle - 1 ); /* search left */
    return lib_gsl_bsearch( array, key, key_deriviation, middle + 1, high ); /* search right */
}
